﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tool4MQ
{
    public partial class Form1 : Form
    {
        private string MQpath, MQpath_save,lookfor;
        private String[] files;
        private int file_count,n=1;
        private DateTime t1, t2;
        private int findfiles = 0;
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void bt_last_move_Click(object sender, EventArgs e)
        {
            //获取移动后的文件路径
            string spitpath = MQpath_save + "\\" + textBox_lookup.Text;
            
            //获取文件下面的所有文件名
          string[] spitfiles = Directory.GetFiles(spitpath, "*.mqmsg", SearchOption.AllDirectories);
            //在文件夹下面创建10个子文件夹
            for (int i = 0; i < 10; i++)
            {
                Directory.CreateDirectory(MQpath_save + "\\" + textBox_lookup.Text + "\\"+i);
            }
            int m = 0;
            for (int i = 0; i < spitfiles.Length; i++)
            {
                
                //分析文件尾号
                string[] files_new = spitfiles[i].Split('_');
                int t = int.Parse(files_new[files_new.Length-3]);
                int yu = t % 10;
                
                switch (yu)
                {
                    default: break;
                    case 0:
                        string OrignFile = spitfiles[i];
                        string[] myfilename = spitfiles[i].Split('\\');
                        string NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\0\\"+ myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 1:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\1\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 2:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\2\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 3:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\3\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 4:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\4\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 5:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\5\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 6:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\6\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 7:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\7\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 8:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\8\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 9:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath_save + "\\" + textBox_lookup.Text + "\\9\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;

                }             

            }
            MessageBox.Show("移动完毕，共移动了" + m + "个文件");
        }

        private void bt_move_Click(object sender, EventArgs e)
        {
           
            //获取文件下面的所有文件名
            string[] spitfiles = Directory.GetFiles(MQpath, "*.mqmsg", SearchOption.AllDirectories);
            //在文件夹下面创建10个子文件夹
            for (int i = 0; i < 10; i++)
            {
                Directory.CreateDirectory(MQpath + "\\" + i);
            }
            int m = 0;
            for (int i = 0; i < spitfiles.Length; i++)
            {

                //分析文件尾号
                string[] files_new = spitfiles[i].Split('_');
                int t = int.Parse(files_new[files_new.Length - 3]);
                int yu = t % 10;

                switch (yu)
                {
                    default: break;
                    case 0:
                        string OrignFile = spitfiles[i];
                        string[] myfilename = spitfiles[i].Split('\\');
                        string NewFile = MQpath + "\\0\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 1:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\1\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 2:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\2\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 3:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\3\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 4:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\4\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 5:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\5\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 6:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\6\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 7:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\7\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 8:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\8\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;
                    case 9:
                        OrignFile = spitfiles[i];
                        myfilename = spitfiles[i].Split('\\');
                        NewFile = MQpath + "\\9\\" + myfilename[myfilename.Length - 1];
                        File.Move(OrignFile, NewFile);
                        m = m + 1;
                        break;

                }

            }
            MessageBox.Show("移动完毕，共移动了" + m + "个文件");
        }

        /// <summary>
        /// 设置MQ消息存放的路径，在这个文件下面的子文件也是会被搜索的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            MQpath = folderBrowserDialog1.SelectedPath;
            label_mqpath.Text = MQpath;
        }

        private void bt_go_Click(object sender, EventArgs e)
        {
            //如果不存在目录，在目标文件夹下创建文件夹
            string mypath = MQpath_save+"\\"+textBox_lookup.Text;
            if (!Directory.Exists(mypath))
            {
                // Create the directory it does not exist.
                Directory.CreateDirectory(mypath);
            
            //记录开始执行时间
            t1 = DateTime.Now;
            label1.Text = "开始时间：" + t1.ToString();
            //获取总数量
            lookfor = textBox_lookup.Text;
            files = Directory.GetFiles(MQpath, "*.mqmsg", SearchOption.AllDirectories);
            file_count = files.Length;
            label_total.Text = file_count.ToString();
            //启动新的线程,使用线程池
                WaitCallback wc1 = new WaitCallback(this.start);
                ThreadPool.QueueUserWorkItem(wc1, "0");

                WaitCallback wc2 = new WaitCallback(this.start);
                ThreadPool.QueueUserWorkItem(wc2, "1");

                WaitCallback wc3 = new WaitCallback(this.start);
                ThreadPool.QueueUserWorkItem(wc3, "2");
                WaitCallback wc4 = new WaitCallback(this.start);
                ThreadPool.QueueUserWorkItem(wc4, "3");
                WaitCallback wc5 = new WaitCallback(this.start);
                ThreadPool.QueueUserWorkItem(wc5, "4");
                WaitCallback wc6 = new WaitCallback(this.start);
                ThreadPool.QueueUserWorkItem(wc6, "5");

                // 获取线程池的最大线程数和维护的最小空闲线程数
                int maxThreadNum, portThreadNum;
                int minThreadNum;
                ThreadPool.GetMaxThreads(out maxThreadNum, out portThreadNum);
                ThreadPool.GetMinThreads(out minThreadNum, out portThreadNum);
                label9.Text = maxThreadNum.ToString() + "/"+ minThreadNum.ToString();
            }
            else
            {
                MessageBox.Show("该路径下已经存在该文件夹，请先删除");
            }
        }

        private void bt_sel_save_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            MQpath_save= folderBrowserDialog1.SelectedPath;
            label_save.Text = MQpath_save;
        }
        /// <summary>
        /// 工作线程
        /// </summary>
        private void start(object o)
        {
           
            for (int i = 0; i < files.Length; i++)
            {
                //对文件名中的消息序号进行截取，为多线程做准备
                //第[9]位置的为顺序号
                string[] files10 = files[i].Split('_');
                int t = int.Parse(files10[files10.Length-3]);
                //判断奇数还是偶数
                int yu = t % 6;
                //配合前面的线程池使用
                if (yu == int.Parse(o.ToString()))
                {
                    textBox1.Text = files[i] + "\r\n" + textBox1.Text;
                    //读取字符串
                    string text = System.IO.File.ReadAllText(@files[i]);
                   
                    int a = text.IndexOf(textBox_lookup.Text);
                    string[] filename = files[i].Split('\\');
                    int fileslen = filename.Length;
                    if (a > 1)
                    {
                        textBox2.Text = files[i] + "\r\n" + textBox2.Text;
                        findfiles = findfiles + 1;
                        //移动到指定位置
                        string OrignFile = files[i];
                        string NewFile = MQpath_save +"\\"+textBox_lookup.Text +"\\"+filename[fileslen - 1];
                        File.Move(OrignFile, NewFile);
                    }
                }                               
            }
            label_thred1.Text = findfiles.ToString();
            
            t2 = DateTime.Now;
            label2.Text = "完成时间：" + t2.ToString();
            TimeSpan t3 = t2 - t1;
            label3.Text = "耗时：" + t3.ToString();
            
        }

    }
}
